package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.ServprodDomain;
import com.xingkeduo.pojo.json.ServprodJson;
import com.xingkeduo.pojo.query.ServprodQuery;
import com.xingkeduo.pojo.table.ServprodTable;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 服务商品管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
//@CrossOrigin
@RestController
@RequestMapping(CMS_URL_PREFIX + "/servprod")
public class ServprodController extends InitController {
    //    private static final Logger LOGGER = LoggerFactory.getLogger(ServprodController.class);
    @Autowired
    private ServprodDomain servprodDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ServprodQuery servprodQuery = ServprodJson.createQueryForServprod(urlParam);
            long id = servprodQuery.getId();
            ServprodTable servprodTable = servprodDomain.getServprodById(id);
            return returnUtil.getSuccess(ServprodJson.toJson(servprodTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid ServprodTable servprodVo, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            servprodVo.setCreatedBy(this.getOperator(request));
            log.info("添加服务接口请求参数:{}", JSON.toJSONString(servprodVo));
            int servprod = servprodDomain.createServprod(servprodVo);
            log.info("添加服务接口返回值:{}", JSON.toJSONString(servprod));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid ServprodTable servprodVo, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            ServprodTable servprodTable = ServprodJson.createTableForModifyServprod(urlParam, servprodVo);
            servprodVo.setUpdatedBy(this.getOperator(request));
            log.info("修改服务接口请求参数:{}", JSON.toJSONString(servprodVo));
            int servprod = servprodDomain.modifyServprod(servprodTable);
            log.info("修改服务接口返回值:{}", JSON.toJSONString(servprod));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ServprodQuery servprodQuery = ServprodJson.createQueryForServprod(urlParam);
            long id = servprodQuery.getId();
            servprodDomain.delServprod(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ServprodQuery servprodQuery = new ServprodQuery();
//            List<String> list = new ArrayList<>(10);
//            if (urlParam.contains("[")) {
//                JSONArray array = JSONArray.parseArray(urlParam);
//                for (int i = 0; i < array.size(); i++) {
//                    String string = array.getString(i);
//                    list.add(string);
//                }
//            }
//            if (list.size() > 0) {
//                servprodQuery.setIds(list);
//            } else {
//                servprodQuery = JSON.parseObject(urlParam, ServprodQuery.class);
//            }
            servprodQuery = JSON.parseObject(urlParam, ServprodQuery.class);
            List<ServprodTable> servprodList = servprodDomain.getServprodList(servprodQuery);

            return returnUtil.getSuccess(ServprodJson.toJson(servprodList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }


}
